<template>
	<!-- 物料出入库导出页面 -->
	<a-card :bordered="false">
		<!-- 顶部筛选 -->
		<div class="table-page-search-wrapper">
			<a-form layout="inline" labelAlign="right">
				<a-row :gutter="48">
					<a-col :md="8" :sm="24">
						<a-form-item label="采购单号">
							<a-input style="width:100%" v-model="queryParam.orderNumber" placeholder="请输入采购单号" />
						</a-form-item>
					</a-col>
					<a-col :md="8" :sm="24">
						<a-form-item label="提交时间">
							<a-range-picker :value-format="'YYYY-MM-DD HH:mm:ss'" style="width:100%" v-model="createTime"
								@change="TimeOk" />
							<!-- <a-range-picker v-model="queryParam.createTime" format="YYYY-MM-DD" /> -->
						</a-form-item>
					</a-col>
					<a-col :md="24" :sm="24">
						<a-form-item label="供  应  商">
							<!-- <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdSupplier">
								<a-radio-button value="">
									全部
								</a-radio-button>
								<a-radio-button :value="id"
									v-for="{ id, name } in $util.getOrgList([this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id, this.$businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id])"
									:key="id">
									{{ name }}
								</a-radio-button>
							</a-radio-group> -->
							<FCOrgRadioGroup1 v-model="queryParam.orgIdSupplier" :filter-types="materialType" />
						</a-form-item>
					</a-col>

					<a-col :md="10" :sm="24">
						<a-form-item label="状态">
							<a-radio-group default-value="" button-style="solid" v-model="queryParam.executeStatus">
								<a-radio-button value="">
									全部
								</a-radio-button>
								<a-radio-button :value="id" v-for="{ id, name } in executeStatusList" :key="id">
									{{ name }}
								</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col :md="10" :sm="24">
						<a-form-item label="采购单类型">
							<a-radio-group default-value="" button-style="solid" v-model="queryParam.subOrderType">
								<a-radio-button value="">
									全部
								</a-radio-button>
								<a-radio-button :value="id" v-for="{ id, name } in subOrderTypeList" :key="id">
									{{ name }}
								</a-radio-button>
							</a-radio-group>
						</a-form-item>
					</a-col>
					<a-col :md="4" :sm="24">
						<a-button type="primary" @click="$refs.table.refresh()">搜索</a-button>
						<a-button
							@click="() => { queryParam = { executeStatus: '' }; createTime = null; $refs.table.refresh() }">重置</a-button>
					</a-col>
				</a-row>
			</a-form>
		</div>
		<div>
			<Export text="采购订单明细导出" v-if="isCaiGouRole() || managerOrFinance()" type="danger" title="采购订单明细"
				url="/emsOrder/cgExport" :obj="{
					...queryParam,
					materialType: materialType,
					orderType: orderType,
				}" />
		</div>
		<!-- 表格 -->
		<s-table ref="table" style="margin-top: 15px;" size="default" :columns="columns" :data="loadData" :alert="true">
			<template slot="executeStatus" slot-scope="text, record">
				<span v-if="record.executeStatus == '1844266818289676290'">
					<a-tag color="#108ee9">待审核</a-tag>
				</span>
				<span v-else-if="record.executeStatus == '1479467252946591746'">
					<a-tag color="#FA8C16">待报价</a-tag>
				</span>
				<span v-else-if="record.executeStatus == '1844268326599143426'">
					<a-tag color="#f50">重新报价</a-tag>
				</span>
				<span v-else-if="record.executeStatus == '1816659510068121600'">
					<a-tag color="#f50">重新提交</a-tag>
				</span>
				<span v-else>
					<a-tag color="#52C41A">审核通过</a-tag>
				</span>
			</template>
			<span slot="subOrderType" slot-scope="text, record">
				{{ record.subOrderType == 1 ? '常规采购' : record.subOrderType == 2 ? '代加工采购' : '--' }}
			</span>
			<span slot="action" slot-scope="text, record">
				<span style="margin: 10px;">
					<a @click="$refs.editForm.readonly(record, record.executeStatus)" v-if="isShowDetailBtn">详情</a>
					<a v-else>-</a>
				</span>
				<span style="margin: 10px;" v-if="record.executeStatus == '1479467252946591746' && isShowQuoteBtn">
					<a @click="$refs.editForm.quote(record)">报价</a>
				</span>
				<span style="margin: 10px;" v-if="record.executeStatus == '1844266818289676290' && isShowAuditBtn">
					<a @click="$refs.editForm.audit(record)">审核</a>
				</span>
				<span style="margin: 10px;" v-if="record.executeStatus == '1844268326599143426' && isShowReQuoteBtn">
					<a @click="$refs.editForm.reQuote(record)">重新报价</a>
				</span>
				<span style="margin: 10px;" v-if="record.executeStatus == '1816659510068121600' && isShowReSubmitBtn">
					<a @click="handleReSubmit(record)"> 重新提交 </a>
				</span>
				<span style="margin: 10px;" v-if="record.executeStatus == '1479467316549017601' && isShowRevoketBtn">
					<a-popover title="撤回" trigger="click">
						<template slot="content">
							<div style="text-align: center;">
								<div style="margin: 5px 0px;">
									<a-button type="danger" @click="handleRevoke(1, record)" :loading="revokeLoading">
										撤销至待报价
									</a-button>
								</div>
								<div style="margin: 5px 0px;">
									<a-button type="danger" @click="handleRevoke(2, record)" :loading="revokeLoading">
										撤销至待编辑
									</a-button>
								</div>
							</div>
						</template>
						<a> 撤回 </a>
					</a-popover>
				</span>
				<span style="margin: 10px;" v-if="isShowDelBtn && record.cgRkStatus == 1">
					<a-popconfirm placement="topRight" title="确认删除？" @confirm="() => handleDelete(record)">
						<a>删除</a>
					</a-popconfirm>
				</span>
			</span>
		</s-table>
		<!--  新增编辑详情弹框  -->
		<edit-form ref="editForm" @ok="$refs.table.refresh()"></edit-form>
		<simple-order-add-form1 ref="simpleOrderAddForm1" @ok="handleOk" />
	</a-card>
</template>

<script>
import { STable } from '@/components'
import { page, purchaseOrderFinish, deleteCGOrder } from '@/api/modular/main/emsorder/emsOrderManage'
import FCOrgRadioGroup1 from '@/components/FcOrgRadioGroup/FCOrgRadioGroup1'
import { getUrlParam, MATERIAL_TYPE_ENUM, ORDER_TYPE_ENUM } from '@/utils/util'
import simpleOrderAddForm1 from '../../emsmaterialpool/emsSimpleOrderAddForm1'
import { page as emsOrderMaterialManagePage } from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'
import {
	revokeCGOrder,//撤回
} from '@/api/modular/main/workbench/purchaseRequestOrder';

import editForm from './editForm.vue';

export default {
	components: {
		STable,
		editForm,
		FCOrgRadioGroup1,
		simpleOrderAddForm1
	},
	watch: {
		routePath() {
			this.$refs.table.refresh()
		}
	},
	mounted() {
		console.log('@@@@', process)
	},
	computed: {
		roleType() {
			return this.routePath.viewState;
		},
		orderType() {
			return this.routePath.orderType;
		},
		ORDER_TYPE_ENUM() {
			return ORDER_TYPE_ENUM;
		},
		materialType() {
			return this.routePath.materialType;
		},
		//操作-详情
		isShowDetailBtn() {
			if (this.roleType == 'material' || this.roleType == 'customized') {//原料/定制料
				if (this.isJSYRole() || this.isCaiGouRole() || this.managerOrFinance()) {
					return true;
				}
			} else if (this.roleType == "packing") {//包材
				if (this.isCaiGouRole() || this.managerOrFinance()) {
					return true;
				}
			} else if (this.roleType == "cable") {
				if (this.managerOrFinance()) {
					return true;
				}
			}
		},
		//操作-报价按钮 线缆:只有财务可报价 其他三个 只有采购可报价
		//操作-重新报价按钮 同报价一个逻辑
		isShowQuoteBtn() {
			if (this.roleType == "cable") {//线缆
				if (this.hasBtnPremission(["emsOrder:quotedPrice:cable"])) {//财务
					return true;
				} else {
					return false;
				}
			} else if (this.roleType != "cable") {//非线缆
				if (this.roleType == "material" && this.hasBtnPremission(["emsOrder:quotedPrice:material"])) {
					return true;
				} else if (this.roleType == "customized" && this.hasBtnPremission(["emsOrder:quotedPrice:customized"])) {
					return true;
				} else if (this.roleType == "packing" && this.hasBtnPremission(["emsOrder:quotedPrice:packing"])) {
					return true;
				} else {
					return false;
				}
			}
		},
		//重新报价
		isShowReQuoteBtn() {
			if (this.roleType == "cable") {//线缆
				if (this.hasBtnPremission(["emsOrder:againQuotedPrice:cable"])) {//财务
					return true;
				} else {
					return false;
				}
			} else if (this.roleType != "cable") {//非线缆
				if (this.roleType == "material" && this.hasBtnPremission(["emsOrder:againQuotedPrice:material"])) {
					return true;
				} else if (this.roleType == "customized" && this.hasBtnPremission(["emsOrder:againQuotedPrice:customized"])) {
					return true;
				} else if (this.roleType == "packing" && this.hasBtnPremission(["emsOrder:againQuotedPrice:packing"])) {
					return true;
				} else {
					return false;
				}
			}
		},
		//操作-重新提交
		isShowReSubmitBtn() {
			if (this.roleType == "material" && this.hasBtnPremission(["emsOrder:againSubmit:material"])) {
				return true;
			} else if (this.roleType == "cable" && this.hasBtnPremission(["emsOrder:againSubmit:cable"])) {
				return true;
			} else if (this.roleType == "customized" && this.hasBtnPremission(["emsOrder:againSubmit:customized"])) {
				return true;
			} else if (this.roleType == "packing" && this.hasBtnPremission(["emsOrder:againSubmit:packing"])) {
				return true;
			} else {
				return false;
			}
		},
		//操作-撤回按钮
		isShowRevoketBtn() {
			if (this.roleType == "material" && this.hasBtnPremission(["emsOrder:revokeCGOrder:material"])) {
				return true;
			} else if (this.roleType == "cable" && this.hasBtnPremission(["emsOrder:revokeCGOrder:cable"])) {
				return true;
			} else if (this.roleType == "customized" && this.hasBtnPremission(["emsOrder:revokeCGOrder:customized"])) {
				return true;
			} else if (this.roleType == "packing" && this.hasBtnPremission(["emsOrder:revokeCGOrder:packing"])) {
				return true;
			} else {
				return false;
			}
		},
		//操作-删除	
		isShowDelBtn() {
			if (this.roleType == "material" && this.hasBtnPremission(["emsOrder:deleteCGOrder:material"])) {
				return true;
			} else if (this.roleType == "cable" && this.hasBtnPremission(["emsOrder:deleteCGOrder:cable"])) {
				return true;
			} else if (this.roleType == "customized" && this.hasBtnPremission(["emsOrder:deleteCGOrder:customized"])) {
				return true;
			} else if (this.roleType == "packing" && this.hasBtnPremission(["emsOrder:deleteCGOrder:packing"])) {
				return true;
			} else {
				return false;
			}
		},
		//操作-审核
		isShowAuditBtn() {
			if (this.roleType == "material" && this.hasBtnPremission(["emsOrder:caiwuAudit:material"])) {
				return true;
			} else if (this.roleType == "cable" && this.hasBtnPremission(["emsOrder:caiwuAudit:cable"])) {
				return true;
			} else if (this.roleType == "customized" && this.hasBtnPremission(["emsOrder:caiwuAudit:customized"])) {
				return true;
			} else if (this.roleType == "packing" && this.hasBtnPremission(["emsOrder:caiwuAudit:packing"])) {
				return true;
			} else {
				return false;
			}
		},
		routePath() {
			let urlParam = this.$util.getUrlParam(this.$route.path);
			console.log(urlParam, "urlParam");

			return urlParam
		},
	},
	data() {
		return {
			queryParam: {},  // 查询参数
			createTime: null,
			revokeLoading: false,
			columns: [
				{
					title: '序号',
					width: '80px',
					customRender: (text, record, index) => `${index + 1}`
				},
				{
					title: '采购单号',
					dataIndex: 'orderNumber'
				},
				{
					title: '供应商',
					dataIndex: 'targetOrgName'
				},
				{
					title: '状态',
					// dataIndex: 'executeStatus',
					align: "center",
					scopedSlots: { customRender: 'executeStatus' }
				},
				{
					title: '提交时间',
					dataIndex: 'createTime',
				},
				{
					title: '提交人',
					dataIndex: 'createUserName',
				},
				{
					title: '采购单类型',
					dataIndex: 'subOrderType',
					scopedSlots: { customRender: 'subOrderType' }
				},

				{
					title: '操作',
					align: 'center',
					width: '250px',
					scopedSlots: { customRender: 'action' }
				}
			],
			// 加载数据方法 必须为 Promise 对象
			loadData: parameter => {
				let query = {
					...parameter,
					...this.queryParam,
					materialType: this.materialType,
					orderType: this.orderType,
					cgApplyType: 1
				}
				return page(query).then((res) => {
					return res.data
				})
			},
			selectedRowKeys: [],
			selectedRows: [],
			executeStatusList: [
				{
					id: '1479467252946591746',
					name: '待报价'
				},
				{
					id: '1844266818289676290',
					name: '待审核'
				},
				{
					id: '1479467316549017601',
					name: '审核通过'
				},
				{
					id: '1844268326599143426',
					name: '重新报价'
				},
				{
					id: '1816659510068121600',
					name: '重新提交'
				},
			],
			subOrderTypeList: [
				{ id: 1, name: "常规采购" },
				{ id: 2, name: "代加工采购" }
			]
		}
	},
	methods: {
		//重新提交
		handleReSubmit(record) {
			console.log(record);
			emsOrderMaterialManagePage({
				pageSize: -1,
				materialType: this.$util.getUrlParam(this.$route.path).materialType,
				orderId: record.orderId,
				sourceOrderMaterialId: 0
			}).then(res => {
				console.log("res@@@", res);

				let list = res.data.rows.map(val => {
					return {
						...val.material,
						number: val.number,
						orderMaterialId: val.orderMaterialId,
					}
				});
				this.$refs.simpleOrderAddForm1.reSubmit({
					orderType: ORDER_TYPE_ENUM.purchaseOrder.id,
					selectedRows: list,
					materialType: this.$util.getUrlParam(this.$route.path).materialType,
					poolType: ORDER_TYPE_ENUM.purchaseOrder.id,
					orderId: record.orderId,
					executeStatus: record.executeStatus,
					orderNumber: record.orderNumber,
					remark: record.remark,
					subOrderType: record.subOrderType,
				})
			})
			return

		},
		//type == 1 撤销至待报价 type == 2 撤回至待编辑
		handleRevoke(type, record) {
			console.log(type, record);
			let that = this;
			this.revokeLoading = true;
			if (type == 1) {
				this.$confirm({
					title: '提示',
					content: '确认要撤回至待报价吗?',
					onOk: () => {
						revokeCGOrder({
							orderId: record.orderId,
							executeStatus: "1844268326599143426",
						}).then(res => {
							console.log(res);

							if (res.code == 200) {
								that.$message.success('撤回成功');
								that.$refs.table.refresh();
								that.revokeLoading = false;
							}
						}).finally(() => {
							that.$refs.table.refresh();
							that.revokeLoading = false;
						})
					},
					onCancel() {
						console.log("取消");

						that.$refs.table.refresh();
						that.revokeLoading = false;
					}
				})
			} else {
				this.$confirm({
					title: '提示',
					content: '确认要撤回至待编辑吗?',
					onOk: () => {
						revokeCGOrder({
							orderId: record.orderId,
							executeStatus: "1816659510068121600",
						}).then(res => {
							if (res.code == 200) {
								that.$message.success('撤回成功');
								that.$refs.table.refresh();
								that.revokeLoading = false;
							}
						}).finally(() => {
							that.$refs.table.refresh();
							that.revokeLoading = false;
						})
					},
					onCancel() {
						that.$refs.table.refresh();
						that.revokeLoading = false;
					}
				})
			}

		},
		handleOk() {
			this.$refs.table.refresh()
		},
		handleDelete(record) {
			deleteCGOrder({
				orderId: record.orderId
			}).then(res => {
				if (res.code == 200) {
					this.$message.success('删除成功');
					this.$refs.table.refresh()
				}
			})
		},
		TimeOk(value) {
			this.queryParam.startTime = value[0].split(" ")[0] + " 00:00:00";
			this.queryParam.endTime = value[1].split(" ")[0] + " 23:59:59";
		},
		onSelectChange(selectedRowKeys, selectedRows) {
			this.selectedRowKeys = selectedRowKeys
			this.selectedRows = selectedRows
		},
	}
}
</script>

<style lang="less">
.table-operator {
	margin-bottom: 18px;
}

button {
	margin-right: 8px;
}
</style>
